let app = getApp();
import { orderSnapshot } from "../../../api";
const parse = require("../../../node_modules/mini-html-parser2");
import { baseImgUrl } from "../../../setting";
Page({
  data: {
    goodsId: '',
    orderId: '',
    goodsInfo: {},
    swiperList: [],
    tabs: [
      { id: 0, title: '商品详情' },
      { id: 1, title: '租赁说明' }
    ],
    activeTabId: 0,
    nodes: [],
    labelList: [],
  },
  onLoad(option) {
    this.setData({
      goodsId: option.goodsId,
      orderId: option.orderId
    })
    this.getDetails()
  },
  getDetails() {
    orderSnapshot(this.data.orderId).then(res => {
      if(res.id){
        let imgs = res.images
        let imgList = []
        if (imgs && imgs.indexOf(",") == -1) {
          imgList.push(imgs)
        } else {
          imgList = imgs ? imgs.split(',') : []
        }
        let contentHtml = res.goods_content.replace(/<img [^>]*src=['"]([^'"]+)[^>]*>/gi, (match, capture) => {
          let newStr = '<img src="' + capture + '" style="width:100%;" alt="" />';
          return newStr;
        });
        parse(contentHtml, (err, nodes) => {
          if (!err) {
            this.setData({
              nodes: nodes,
            })
          }
        })
        const rentArr = res.rent_label.replace(/,/g, '，').split('，').filter((p, idx) => p != '' && idx < 2);
        this.setData({
          goodsInfo: res,
          labelList: rentArr,
          swiperList: imgList.map(item => ({ img: item }))
        })
      }
    })
  },
  onParBanner(e) {
    const urls = this.data.swiperList.map(item => baseImgUrl + item.img);
    my.previewImage({
      current: e.index,
      urls: urls
    });
  },
  toGoodsDetails(){
    my.navigateTo({ url: '../productDetails/productDetails?goodsId=' + this.data.goodsId+'&sale_user_id='+app.globalData.sale_user_id })
  },
  handleTabClick({ index, tabsName }) {
    this.setData({
      [tabsName]: index,
    });
  },

});
